$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeSource" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeInput" );

SmitearWebGL.Objects.Parser.Collada.DaeMorph = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, node )
	{
		this.source = "";
		this.targets = null;
		this.weights = null;
		this.method = "";

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeMorph" );
	}
	,
	read : function( node )
	{
		$THROW( node.localName == SmitearWebGL.Objects.Parser.Collada.Const.DAE_CONTROLLER_MORPH_ELEMENT,
		"expected a '" + SmitearWebGL.Objects.Parser.Collada.Const.DAE_CONTROLLER_MORPH_ELEMENT + "' element" );

		this._super( node );

		this.source = this.getAttribute(node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_SOURCE_ATTRIBUTE);
		this.method = this.getAttribute(node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_METHOD_ATTRIBUTE) == SmitearWebGL.Objects.Parser.Collada.DaeMorph.METHOD_RELATIVE ? SmitearWebGL.Objects.Parser.Collada.DaeMorph.METHOD_RELATIVE : SmitearWebGL.Objects.Parser.Collada.DaeMorph.METHOD_NORMALIZED;

		$LOG( "reading morph, source: " + this.source + " method: " + this.method);

		// exactly one targets element
		var targetNode = this.getNode(node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_TARGETS_ELEMENT);
		this.targets = this.weights = null;
		var inputList = this.getNodeList(targetNode, SmitearWebGL.Objects.Parser.Collada.Const.DAE_INPUT_ELEMENT);

		for ( var i = 0; i < inputList.length; ++ i )
		{
			var inputNode = inputList[i];
			var input = new SmitearWebGL.Objects.Parser.Collada.DaeInput( this.document, inputNode );

			switch( input.semantic )
			{
				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_TARGET_MORPH_INPUT:
					this.targets = this.document.sources[ input.source ];
					break;

				case SmitearWebGL.Objects.Parser.Collada.Const.DAE_WEIGHT_MORPH_INPUT:
					this.weights = this.document.sources[ input.source ];
					break;

				default:
					break;
			}
		}
		
		$THROW( this.targets, "Invalid morph, could not find morph-targets" );
		$THROW( this.weights, "Invalid morph, could not find morhp-weights!"  );
	}
}
);

SmitearWebGL.Objects.Parser.Collada.DaeMorph.METHOD_NORMALIZED = "NORMALIZED";
SmitearWebGL.Objects.Parser.Collada.DaeMorph.METHOD_RELATIVE = "RELATIVE";